package com.hsd.znsh.controller.app;

import com.hsd.znsh.entity.*;
import com.hsd.znsh.exception.ZnshException;
import com.hsd.znsh.pojo.output.BatchVO;
import com.hsd.znsh.pojo.output.FarmVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.properties.SecurityProperties;
import com.hsd.znsh.service.*;
import com.hsd.znsh.service.impl.BaseServiceImpl;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-02 15:40:27
 */
@RequestMapping("/app/farm")
@RestController
public class APPFarmController {

    @Autowired
    private BatchService batchService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private FarmTypeService farmTypeService;

    @Autowired
    private FarmService farmService;

    @Autowired
    private ProductService productService;

    @Autowired
    private PickService pickService;


    @GetMapping("/getBatchListByHostSn")
    public ResultVO getBatchListByHostSn(String hostSn,@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        List<BatchVO> list;
        if(hostSn==null||hostSn.equals("")){
            list= batchService.getBatchListByUser(user.getCreateUserId());
        }else{
            list= batchService.getBatchListByHostSn(hostSn);
        }
        return ResultVOUtil.success(list);
    }

    @PostMapping("/addBatch")
    public ResultVO addBatch(@Valid Batch batch, BindingResult bindingResult,String hostSn){
        ValidatorUtil.valid(bindingResult);
        batchService.addBatch(batch,hostSn);
        return ResultVOUtil.success();
    }

    @PostMapping("/deleteBatch")
    public ResultVO deleteBatch(Integer id){
        batchService.deleteBatch(id);
        return ResultVOUtil.success();
    }

    @PostMapping("/finishBatch")
    public ResultVO finishBatch(Integer id){
        batchService.finishBatch(id);
        return ResultVOUtil.success();
    }

    @GetMapping("/getProductList")
    public ResultVO getProductList(@RequestHeader("token") String token){
        User user=redisService.get(token,User.class);
        List<Product> list=productService.getProductListByAPPUser(user);
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getFarmTypeList")
    public ResultVO getFarmTypeList(){
        List<FarmType> list=farmTypeService.getFarmTypeList();
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getFarmListByBatchId")
    public ResultVO getFarmListByBatchId(Integer batchId,@RequestParam(required = false) Integer farmTypeId){
        List<FarmVO> list=farmService.getFarmListByBatchId(batchId,farmTypeId);
        return ResultVOUtil.success(list);
    }

    @PostMapping("/addFarm")
    public ResultVO addFarm(@RequestHeader("token") String token,
                            @RequestParam(value = "file",required = false) MultipartFile multipartFile,
                            @Valid Farm farm,BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        User user=redisService.get(token,User.class);
        farmService.addFarm(multipartFile,farm,user.getId());
        return ResultVOUtil.success();
    }

    @PostMapping("/deleteFarm")
    public ResultVO deleteFarm(Integer id){
        farmService.deleteFarm(id);
        return ResultVOUtil.success();
    }

    @GetMapping("/getPickListByBatchId")
    public ResultVO getPickListByBatchId(Integer batchId){
        List<Pick> list=pickService.getPickListByBatchId(batchId);
        return ResultVOUtil.success(list);
    }

    @PostMapping("/addPick")
    public ResultVO addPick(@Valid Pick pick,BindingResult bindingResult,Integer batchId){
        ValidatorUtil.valid(bindingResult);
        pickService.addPick(pick,batchId);
        return ResultVOUtil.success("采摘成功");
    }




}
